# CartPage.py
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class CartPage:
    def __init__(self, driver):
        # 初始化购物车页面对象
        self.driver = driver
        self.url = 'http://your-ecommerce-app/cart'
        self.add_to_cart_button = (By.ID, 'add-to-cart-button')
        self.remove_from_cart_button = (By.ID, 'remove-from-cart-button')
        self.update_quantity_input = (By.ID, 'update-quantity-input')
        self.update_cart_button = (By.ID, 'update-cart-button')
        self.cart_item_count = (By.ID, 'cart-item-count')
        self.empty_cart_message = (By.ID, 'empty-cart-message')
        self.item_name = (By.CLASS_NAME, 'item-name')
        self.item_price = (By.CLASS_NAME, 'item-price')
        self.total_price = (By.ID, 'total-price')
        self.proceed_to_checkout_button = (By.ID, 'proceed-to-checkout-button')

    # 导航到购物车页面
    def navigate(self):
        self.driver.get(self.url)

    # 添加商品到购物车
    def add_to_cart(self):
        WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable(self.add_to_cart_button)).click()

    # 从购物车移除商品
    def remove_from_cart(self):
        WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable(self.remove_from_cart_button)).click()

    # 更新购物车内商品的数量
    def update_quantity(self, quantity):
        WebDriverWait(self.driver, 10).until(EC.presence_of_element_located(self.update_quantity_input)).clear()
        WebDriverWait(self.driver, 10).until(EC.presence_of_element_located(self.update_quantity_input)).send_keys(quantity)
        WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable(self.update_cart_button)).click()

    # 获取购物车内的商品数量
    def get_item_count(self):
        item_count = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located(self.cart_item_count))
        return int(item_count.text)

    # 检查购物车是否为空
    def is_cart_empty(self):
        return WebDriverWait(self.driver, 10).until(EC.presence_of_element_located(self.empty_cart_message)).is_displayed()

    # 获取购物车内的商品名称
    def get_item_names(self):
        items = WebDriverWait(self.driver, 10).until(EC.presence_of_all_elements_located(self.item_name))
        return [i.text for i in items]

    # 获取购物车内的商品价格
    def get_item_prices(self):
        prices = WebDriverWait(self.driver, 10).until(EC.presence_of_all_elements_located(self.item_price))
        return [p.text for p in prices]

    # 获取购物车总价
    def get_total_price(self):
        total_price = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located(self.total_price))
        return float(total_price.text.replace('$', ''))

    # 点击结算按钮
    def click_proceed_to_checkout(self):
        WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable(self.proceed_to_checkout_button)).click()